﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class PlcReadDbInfoRepository:BaseRepository<PlcReadDbInfo>, IPlcReadDbInfoRepository
    {
        public PlcReadDbInfoRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<PageDto<PlcReadDbInfoDto>> GetPlcReadDbInfoAsync(Expression<Func<PlcReadDbInfo, JobInfo, PlcConnectionInfo, bool>> exp, PageModel page)
        {
            PageDto<PlcReadDbInfoDto> pageDto = new PageDto<PlcReadDbInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<PlcReadDbInfo, JobInfo, PlcConnectionInfo>((a, b, c) => new JoinQueryInfos(
                JoinType.Left, a.JobID == b.ID,
                JoinType.Left, a.PlcConnectionID == c.ID))
                .Where(exp)
                .Select((a, b, c) => new PlcReadDbInfoDto
                {
                    ID = a.ID,
                    PlcName = a.PlcName,
                    JobID = a.JobID,
                    PlcConnectionID = a.PlcConnectionID,
                    DbCode = a.DbCode,
                    DbStart = a.DbStart,
                    DbLength = a.DbLength,
                    IsHeartbeat = a.IsHeartbeat,
                    IsEnable = a.IsEnable,
                    ProcessModuleType = a.ProcessModuleType,
                    ProcessModuleName = a.ProcessModuleName,
                    JobName = b.JobName,
                    ProgrameCode = b.ProgrameCode,
                    PlcConnectionIP = c.IP,
                    PlcCpuType = c.PlcCpuType.ToString(),
                    EquipmentCode=a.EquipmentCode
                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);

            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }
    }
}
